package com.ziyun.erp.web.modules.goods.controller;

import java.util.Arrays;
import java.util.List;
import java.util.Map;

import com.ziyun.erp.common.utils.UserUtils;
import io.renren.common.validator.ValidatorUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import com.ziyun.erp.modules.goods.entity.GoodsProductEntity;
import com.ziyun.erp.modules.goods.service.GoodsProductService;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;



/**
 * 产品表
 *
 * @author
 * @email
 * @date 2018-08-21 08:46:29
 */
@RestController
@RequestMapping("goods/goodsproduct")
public class GoodsProductController {
    @Autowired
    private GoodsProductService goodsProductService;

    /**
     * 列表
     */
    @RequestMapping("/list")
//    @RequiresPermissions("goods:goodsproduct:list")
    public R list(@RequestParam Map<String, Object> params){
        PageUtils page = goodsProductService.queryPage(params);
        return R.ok().put("page", page);
    }


    /**
     * 信息
     */
    @RequestMapping("/info/{productId}")
//    @RequiresPermissions("goods:goodsproduct:info")
    public R info(@PathVariable("productId") Long productId){
        GoodsProductEntity goodsProduct = goodsProductService.selectById(productId);
        return R.ok().put("goodsProduct", goodsProduct);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    @RequiresPermissions("goods:goodsproduct:save")
    public R save(@RequestBody GoodsProductEntity goodsProduct){
        goodsProduct.setCreateName(UserUtils.getCurentUser().getUsername());
        goodsProductService.insert(goodsProduct);
        return R.ok();
    }

    /**
     * 修改
     */
    @RequestMapping("/update")
    @RequiresPermissions("goods:goodsproduct:update")
    public R update(@RequestBody GoodsProductEntity goodsProduct){
        ValidatorUtils.validateEntity(goodsProduct);
        goodsProductService.updateAllColumnById(goodsProduct);//全部更新
        return R.ok();
    }

    /**
     * 删除
     */
    @RequestMapping("/delete")
    @RequiresPermissions("goods:goodsproduct:delete")
    public R delete(@RequestBody Long[] productIds){
        goodsProductService.deleteBatchIds(Arrays.asList(productIds));
        return R.ok();
    }

    /**
     * 列表
     */
    @RequestMapping("/selectProductByGoodsId")
    public R selectProductByGoodsId(@RequestBody Map map){
//        List<GoodsProductEntity> skuTableData = goodsProductService.selectByMap(map);
        List<GoodsProductEntity> skuTableData = goodsProductService.selectProductProperties(map);
        return R.ok().put("skuTableData",skuTableData);
    }

    /**
     * 信息
     */
    @RequestMapping("/getGoodsType/{productId}")
//    @RequiresPermissions("goods:goodsproduct:info")
    public R getGoodsType(@PathVariable("productId") Long productId){
        String goodsType = goodsProductService.getGoodsTypeByProductId(productId);
        Boolean type = goodsProductService.getMaterialType(productId);

        return R.ok().put("goodsType", goodsType).put("type",type);
    }


}
